package com.ys.controller.web.finance;

import com.ys.config.MenuId;
import com.ys.entity.base.TbCountry;
import com.ys.entity.business.TpAmazonShopExchangeRate;
import com.ys.entity.finance.TcwPartnerWbAccount;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/web/finance/tcwPartnerWbAccount")
public class WebTcwPartnerWbAccountController {
	

	@Autowired
	private CommonApiService<TcwPartnerWbAccount> commonApiService;
	
	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		try {
			//生成编码
			String codeTop = "HZWB";
			String code = "";
			TcwPartnerWbAccount tcwPartnerWbAccountMax = new TcwPartnerWbAccount().setOrderby("A.Id DESC").queryFirst();
			if(tcwPartnerWbAccountMax!=null && tcwPartnerWbAccountMax.getCode()!=null ){
				long id = tcwPartnerWbAccountMax.getId();
				long codeNum = id ;//编码从0开始，比id少1
				String codeStr = codeNum+"";
				if(codeNum<10000){
					codeStr = String.format("%0"+5+"d", codeNum);//小于3位在前面补0
				}
				code = codeTop+codeStr;
			}else {
				code = "HZWB00001";
			}
			return commonApiService.toAdd(model,new TcwPartnerWbAccount().setCode(code).setStatus(1).setCreateTime(new Date()), MenuId.TCW_PARTNERWBACCOUNT, null, null);

		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}



	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		if (params.get("id") == null){
			return W.error(model, "id不能为空");
		}

		Long id = Long.valueOf((params.get("id").toString()));

		if (new TcwPartnerWbAccount().setId(id).count() == 0){
			return W.error(model, "该合作商外币信息不存在!");
		}
		TcwPartnerWbAccount tcwPartnerWbAccount = new TcwPartnerWbAccount().setId(id).queryFirst();

		TpAmazonShopExchangeRate tpAmazonShopExchangeRate = new TpAmazonShopExchangeRate().setId(tcwPartnerWbAccount.getExchangeRateId()).queryFirst();

		//国家
		TbCountry tbCountry = new TbCountry().setCode(tcwPartnerWbAccount.getCountryCode()).queryFirst();

		Map<String,Object> extraParam = new HashMap<>();

		extraParam.put("exchangeRateCode",tpAmazonShopExchangeRate.getCode());
		extraParam.put("countryName",tbCountry.getName());

		try {
			return commonApiService.toEdit(model, id, new TcwPartnerWbAccount(), MenuId.TCW_PARTNERWBACCOUNT, extraParam, null);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}

}
